package Procedure1;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.*;

public class Test {
    public static void main(String[] args) throws InterruptedException {

        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);

        pool.scheduleWithFixedDelay(new Runnable() {
            public void run() {
                result();
            }
        }, 1, 1, TimeUnit.SECONDS);
    }
//
    public static void result() {
        int success = 0;   //计算成功次数
        int fail = 0;      //计算失败次数
        long stime = System.currentTimeMillis();//计算开始时间
        try {
            //缓冲区读取内容，避免中文乱码
            BufferedReader reader = new BufferedReader(new FileReader("D:\\kaifamiao\\XIANMU\\untitled1\\src\\Procedure1\\server3.txt"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                InetAddress address = InetAddress.getByName(line);
                System.out.println();
                if (address.isReachable(5000)) {  //判定节点是否可达
                    System.out.printf("%-20s", line);
                    System.out.printf("%-10s", "能够访问");
                    success++;
                } else {
                    System.out.printf("%-20s", line);
                    System.out.printf("%-10s", "无法访问");
                    fail++;
                }
            }
            reader.close();
        } catch (UnknownHostException e) {
            System.out.println("异常");
            fail++;
        } catch (IOException e) {
            e.printStackTrace();
        }
        long etime = System.currentTimeMillis();//计算结束时间
        System.out.println();
        System.out.println("执行结果:");
        System.out.println("成功:" + success + "次 "+ "失败:" + fail + "次" + "\t" + "执行时长:" + (etime - stime) + "毫秒");

    }
}




